package com.android.nativ;

import android.content.Context;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.Log;

import com.unity3d.player.UnityPlayer;

public class Vibration {
    public void triggerVibration(int type) {
        Context context = UnityPlayer.currentActivity.getApplicationContext();
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

        if (vibrator != null && vibrator.hasVibrator()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                handleModernVibration(vibrator, type);
            } else {
                handleLegacyVibration(vibrator, type);
            }
        }
    }

    @android.annotation.TargetApi(Build.VERSION_CODES.O)
    private void handleModernVibration(Vibrator vibrator, int type) {
        Log.d("DEVICE_DEBUG", "设备API级别: " + Build.VERSION.SDK_INT);
        switch (type) {
            case 0: // Light (类似iOS Selection)
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    vibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK));
                } else {
                    // API 29以下回退到短震动
                    vibrator.vibrate(VibrationEffect.createOneShot(10, 70));
                }
                break;

            case 1: // Medium (类似iOS Light Impact)
                vibrator.vibrate(VibrationEffect.createOneShot(30, 90));
                break;

            case 2: // Heavy (类似iOS Medium Impact)
                vibrator.vibrate(VibrationEffect.createOneShot(45, 120));
                break;

            case 3: // Soft (类似iOS Soft Alert)
                vibrator.vibrate(VibrationEffect.createWaveform(
                        new long[]{0, 10},  // 等待0ms后震动10ms
                        new int[]{0,80},      // 振幅80（范围1-255）
                        -1                  // 不重复
                ));
                break;

            case 4: // Rigid (类似iOS Rigid Impact)
                vibrator.vibrate(VibrationEffect.createOneShot(20, 140));
                break;

            case 5: // Success（类似iOS Success）
                vibrator.vibrate(VibrationEffect.createWaveform(
                        new long[]{0, 10, 100, 10}, // 两次短震动
                        new int[]{0,100, 0, 140},
                        -1
                ));
                break;
            case 6: // Warning（类似iOS Warning）
                vibrator.vibrate(VibrationEffect.createWaveform(
                        new long[]{0, 10, 200, 15}, // 长-短模式
                        new int[]{0,170, 0, 170},
                        -1
                ));
                break;
            case 7: // Error（类似iOS Error）
                vibrator.vibrate(VibrationEffect.createWaveform(
                        new long[]{0, 15, 100, 15, 100, 15}, // 三次短脉冲
                        new int[]{0,120, 0, 120, 0, 120},
                        -1
                ));
                break;
            default:
                break;
        }
    }

    // 旧版本处理（效果会打折扣）
    private void handleLegacyVibration(Vibrator vibrator, int type) {
        switch (type) {
            case 0: // Light
                vibrator.vibrate(10);
                break;
            case 1: // Medium
                vibrator.vibrate(20);
                break;
            // 其他类型需要简化...
            default:
                vibrator.vibrate(25);
        }
    }
}